uni.$u.http.setConfig((config) => {
	config.baseURL = `https://gcsq.top/api`;
	return config
})
let opt = {
	lang: null,
	debug: true,
	chainId: '0x38',
	walletAddress:'',
	inviteCode:'',
	async checkLogin() {
		let token = uni.getStorageSync("token")
		if (!token || token == '') {
			let invite_code = this.getQueryParam("inviteCode")
			this.inviteCode = invite_code;
			await this.doLogin(invite_code)
		} else {
			let accounts = await ethereum.request({
				method: 'eth_requestAccounts'
			})
			let address = accounts[0];
			this.walletAddress = address;
		}
		// ethereum.on('accountsChanged',  (accounts)=>{
		// 	uni.removeStorageSync("token")
		// 	this.walletAddress = accounts[0];
		// 	this.doLogin(this.inviteCode)
		// })
	},
	async getAddress() {
		let accounts = await ethereum.request({
			method: 'eth_requestAccounts'
		})
		let address = accounts[0];
		return address;
	},
	async connect(invite_code) {
		let _chainId = await ethereum.request({
			method: 'eth_chainId'
		});
		console.log(_chainId)
		if (_chainId !== this.chainId) {
			await ethereum.request({
				method: 'wallet_switchEthereumChain',
				params: [{
					chainId: this.chainId
				}],
			});
		}
		let accounts = await ethereum.request({
			method: 'eth_requestAccounts'
		})
		console.log(accounts)
		let address = accounts[0];
		this.walletAddress = address;
		const signature = await ethereum.request({
			method: 'personal_sign',
			params: ['Signing Password', address, 'Signing Password'],
		});
		await this.postLogin(address, invite_code, signature)
	},
	async doLogin(invite_code) {
		uni.showLoading({
			title: this.lang.t('正在登陆...')
		})
		if (typeof window.ethereum == 'undefined' && this.debug == false) {
			uni.hideLoading()
			uni.showModal({
				showCancel: false,
				content: this.lang.t('请在DAPP中打开')
			})
			return;
		}
		if (this.false) {
			let address = '0x9105abc55bd4d624478a8f9c48c8d33ef0bff839';
			let signature ='7d3c90bc209e47b37933b9255ef2bf15';
			await this.postLogin(address, invite_code, signature)
		} else {
			try{
				await this.connect(invite_code)
			}catch(e){
				uni.hideLoading()
			}
		}

	},
	async postLogin(address, invite_code, signature) {
		try {
			let res = await this.post('user/login', {
				address: address,
				invite_code: invite_code,
				signature: signature
			})
			uni.hideLoading();
			uni.setStorageSync('token', res.data.token);
			uni.setStorageSync('userInfo', JSON.stringify(res.data.user));
		} catch (e) {
			uni.hideLoading()
		}
	},
	getQueryParam(key) {
		var query = location.search.substring(1)
		var arr = query.split('&')
		for (var i = 0; i < arr.length; i++) {
			var pair = arr[i].split('=')
			if (pair[0] == key) {
				return pair[1]
			}
		}
		return undefined
	},
	post(url, params, isLoadding) {
		console.log(this.lang.locale)
		return new Promise((resolve, reject) => {
			if (isLoadding) {
				uni.showLoading({
					title: this.lang.t('加载中...')
				})
			}
			let token = uni.getStorageSync("token")
			let lang = this.lang.locale
			if(this.lang.locale == 'zh-Hans'){
				lang = 'zh-cn';
			}
			uni.$u.http.post(url, params,{
				params: {lang:lang},
				header: {TOKEN:token},
			}).then(res => {
				console.log(res)
				if (isLoadding) {
					uni.hideLoading()
				}
				if (res.data.code == 1) {
					return resolve(res.data)
				}
				uni.showToast({
					title: res.data.msg,
					icon: 'none'
				})
				return reject(res.data.msg)

			}).catch(err => {
				console.log(err)
				if (isLoadding) {
					uni.hideLoading()
				}
				uni.removeStorageSync("token")
				this.checkLogin();
				// return reject(err.data.msg)
				return reject(err)
			})
		});

	}
}

export default opt;